#include "stdafx.h"

#define VERSION_STRING              _T("1.0.0.1")
#define MAX_BUFFER                  32

#pragma comment (lib, "winmm.lib")

/* Print help information. */
static void PrintHelp ();

int _tmain ( int argc, _TCHAR* argv[] )
{
    unsigned long       nNewLineCount;
    unsigned long       nSystemSecounds, nSystemMinutes, nSystemHours;
    MMTIME              mmSystem;
    TCHAR               chBuffer[MAX_BUFFER];


    // print help info:
    if ( argc < 2 || ( argc == 2 && 
        ( _tcscmp ( argv[ 1 ], _T("/?") ) == 0
            || _tcscmp ( argv[ 1 ], _T("--help") ) == 0
            || _tcscmp ( argv[ 1 ], _T("/help") ) == 0 ) ) )
        {
            PrintHelp ();
            return 0;
        }

    // for all existing paramters, check them and preform specified action:
    for ( int i = 1; i < argc; i++ )
    {
        if ( ( argv[i][0] == _T('/') || argv[i][0] == _T('-') )
                && argv[i][2] == _T('\0') )
        {
            // discover the parameter:
            switch ( argv[i][1] )
            {
                case _T('e'):
                    _tsystem ( _T("cls") );
                    break;
                case _T('r'):
                    _tprintf ( _T("\r") );
                    break;
                case _T('n'):
                    if ( i < argc - 1 )
                            nNewLineCount = _ttol ( argv[i+1] );
                    else    nNewLineCount = 0;
                    if ( nNewLineCount == 0 )
                            nNewLineCount = 1;
                    else    i++;
                    for ( unsigned long j = 0; j < nNewLineCount; j++ )
                        _tprintf ( _T("\r\n") );
                    break;
                case _T('d'):
                    _tstrdate_s ( chBuffer, MAX_BUFFER );
                    _tprintf ( _T("%s"), chBuffer );
                    break;
                case _T('c'):
                    _tstrtime_s ( chBuffer, MAX_BUFFER );
                    _tprintf ( _T("%s"), chBuffer );
                    break;
                case _T('s'):
                    if ( SUCCEEDED ( timeGetSystemTime ( &mmSystem, sizeof ( mmSystem ) ) ) )
                    {
                        nSystemSecounds = mmSystem.u.ms / 1000;
                        nSystemMinutes  = nSystemSecounds / 60;
                        nSystemHours    = nSystemMinutes / 60;
                        _tprintf ( _T("%d:%02d:%02d"), nSystemHours, nSystemMinutes % 60, nSystemSecounds % 60 );
                    }
                    else
                    {
                        _tprintf ( _T("Error while acquiring system time.\r\n") );
                        return 3;
                    }
                    break;
                default:
                    _tprintf ( _T("Invalid paramter: %c%c.\r\n"), argv[i][0], argv[i][1] );
                    return 2;
            }
        }
        else 
        {
            _tprintf ( _T("Invalid format for paramter: %s.\n"), argv[i] );
            return 1;
        }
    }

    return 0;
}

static void PrintHelp ()
{
    _tprintf ( _T("Pretorianie 2008 (C)\r\n") );
    _tprintf ( _T("Reformatter v%s\r\n\r\n"), VERSION_STRING );
    _tprintf ( _T(" This utility prints special characters or specified system info.\r\n\r\n") );
    _tprintf ( _T("Usage:\r\n") );
    _tprintf ( _T("\treform.exe [options]\r\n\r\n") );
    _tprintf ( _T(" - /e      - clean the screen\r\n") );
    _tprintf ( _T(" - /n [xx] - insert given number of new-lines\r\n") );
    _tprintf ( _T(" - /r      - return cursor to the line start,\r\n") );
    _tprintf ( _T(" - /d      - print current date\r\n") );
    _tprintf ( _T(" - /c      - print current time\r\n") );
    _tprintf ( _T(" - /s      - print time of latest system run\r\n") );
}

